/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * introduction:
 *  蓝牙串口驱动
 */

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include "hi_io.h"
#include "hi_uart.h"

#include "iot_uart.h"
#include "bluetooth.h"
#define UART_TIMEOUT 10

int Bluetooth_Init()
{
    hi_io_set_func(BLUE_RX_1, HI_IO_FUNC_GPIO_1_UART1_RXD);
    hi_io_set_func(BLUE_TX_0, HI_IO_FUNC_GPIO_0_UART1_TXD);

    hi_uart_attribute my_uart1;
    my_uart1.baud_rate = 115200;
    my_uart1.data_bits = 8;
    my_uart1.parity = HI_UART_PARITY_NONE;
    my_uart1.stop_bits = 1;

    hi_uart_extra_attr my_extra;
    my_extra.rx_block = HI_UART_BLOCK_STATE_NONE_BLOCK;

    my_extra.tx_block = HI_UART_BLOCK_STATE_NONE_BLOCK;

    return hi_uart_init(HI_UART_IDX_1, &my_uart1, NULL);
}

int Bluetooth_read(char *get_data, int data_len)
{
    if (get_data == NULL)
    {
        return -1;
    }
    int ret;
    ret = hi_uart_read_timeout(HI_UART_IDX_1, get_data, data_len, UART_TIMEOUT); // 限时读取 防堵塞
    return ret;
}
int Bluetooth_write(char *get_data, int data_len)
{
    if (get_data == NULL)
    {
        return -1;
    }
    int ret;
    ret = hi_uart_write(HI_UART_IDX_1, get_data, data_len);
    return ret;
}
